#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
#include<math.h>

int parseInt(char* p,int s,int e){
	int i = 0;
	int r = 0;
	for(i = s;i<= e;++i){
		r += (int)(p[i] - '0') * pow(10,e - s);
	}
	return r;
}

int compute(char* p ,int* r,int m){
	int len = strlen(p);
	int i = 0,n=0,s=0,st = 0;
	for(i = 0;i < len + 1; ++i){
		if(p[i] <= '9' && p[i] >= '0'){
			if(s == 0){
				st = i;
				s = 1;
			}
		}else{
			if(s == 1){
				r[n++] = parseInt(p,st,i-1);
				s = 0;
			}
		}
	}
	s = 0;
	st = 0;
	for(i = 0;i < m;++i){
		for(n = i + 1;n < m;++n){
			if(r[n] > r[i]){
				st = r[n];
				r[n] = r[i];
				r[i] = st;
			}
		}
		s += i * r[i];
	}
	return s;
}

int main(){
	int n = 0,m = 0;
	int* r = NULL;
	char* p = (char*)malloc(sizeof(char) * 2048);
	scanf("%d",&n);
	while(n--){
		scanf("%d",&m);
		memset(p,0,sizeof(char) * 2048);
		//fflush(stdin);
		scanf(" %[^\n]",p);
		r = (int*)malloc(sizeof(int)*m);
		printf("%d\n",compute(p,r,m));
		free(r);
	}
	free(p);
	return 0;
}
